﻿using FoodModel;
using J.FoodManageGet.Application.Command.TableNumCommand;
using J.FoodRepository.InterFace;
using MediatR;
using System.Drawing.Printing;

namespace J.FoodManageGet.Application.CommandHandler.TableNumCommand
{
    /// <summary>
    /// 桌号显示
    /// </summary>
    public class TableNumShowCommandHandler : IRequestHandler<TableNumShowCommand, (int totalCount, int PagCount,List<TableNumModel> list)>
    {
        private readonly ITableNumRepository _tableNumRepository;

        public TableNumShowCommandHandler(ITableNumRepository tableNumRepository)
        {
            _tableNumRepository = tableNumRepository;
        }
        public async Task<(int totalCount,int PagCount,List<TableNumModel> list)> Handle(TableNumShowCommand request, CancellationToken cancellationToken)
        {
            var list = await _tableNumRepository.GetAsync(x =>
           (string.IsNullOrEmpty(request.TableNum) || x.TableNum.Contains(request.TableNum)));

            int PageTotal = list.Count;
            int PageCount = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize);
            list = list.OrderByDescending(x=>x.TableNumId).Skip((request.PageIndedx - 1) * request.PageSize).Take(request.PageSize).ToList();
            return (PageTotal, PageCount, list);
        }
    }
}
